package com.sx.wechat.command.api;

import com.sx.core.vo.JsonReaderResponse;
import com.sx.wechat.command.cmd.AbstractCommand;
import com.sx.wechat.security.AuthenticationHolder;
import com.sx.wechat.security.Principal;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by lihao on 2018/9/25.
 */
@Component("scan_device")
public class Scan_device extends AbstractCommand {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public Object executeApi(HttpServletRequest request, HttpServletResponse response) {
        Principal principal= AuthenticationHolder.getCurrent();
        String deviceId=obtain(request,"deviceId");
        List<Map<String,Object>> list = jdbcTemplate.queryForList("SELECT * FROM t_device WHERE id=?",deviceId);
        if(list.isEmpty()){
            return new JsonReaderResponse("未找到相关设备",101);
        }
        Map<String,Object> map=list.get(0);
        if(StringUtils.equals("1", ObjectUtils.toString(map.get("status")))){
            return new JsonReaderResponse("该设备正在进行",101);
        }
        List<Map<String,Object>> orders=jdbcTemplate.queryForList("SELECT " +
                "o.*\n " +
                "FROM t_order_rec_stu r " +
                "JOIN t_order o ON r.orderid=o.id " +
                "WHERE r.stuid=?",principal.getId());


        return new JsonReaderResponse(orders);
    }
}
